#!/usr/bin/env python
#
# error.py is part of pyStrips
# Copyright (C) - 2009 - Juan B. Cabral <jbcabral_89@yahoo.com.ar>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

################################################################################
# DOCS
################################################################################

'''Error handler for pyStrips'''


################################################################################
# IMPORTS
################################################################################

import re

import common


################################################################################
# Constants
################################################################################

SYNTAX_ERROR = "Syntax Error"
SEMANTIC_ERROR = "Semantic Error"
INTERNAL_ERROR = "Internal Error"


################################################################################
# PYSTRIPS ERROR CLASS
################################################################################

class PyStripsError(Exception):
    def __init__(self, msg, err_type, statement):
        self.err_type = err_type
        self.msg = self._clean_str(msg)
        self.statement = self._clean_str(statement)
        self.line_number = 0
      
    def calculate_line_number(self, src):
        re_st = re.compile("%s(\s)*($|%s)*" % (self.statement, common.RW_AND))
        for idx, line in enumerate(src.splitlines()):
            line = _clean_str(line)
            if re_st.search(line):
                self.line_number = idx + 1
                break
    
    def _clean_str(self, str_tc):
        # clean the possibles {} and [] chars in correct
        # yaml parser but incorrect strips.
        common.RE_INVALID_CHARS.sub("", str_tc.strip())
    
    def __repr__(self):
        return str(self)

    def __str__(self):
        msg = []
        msg.append("ERROR FOUND!!!")
        msg.append("Error Type> %s" % self.err_type)
        if self.statement:
            msg.append("Statement> '%s'" % self.statement)
        if self.line_number:
            msg.append("Line Number> %i" % self.line_number)
        msg.append("Description> %s" % self.msg)
        if self.err_type == INTERNAL_ERROR:
            msg.append("PLEASE REPORT THIS BUG!!!")
        return "\n".join(msg)


################################################################################
# MAIN
################################################################################
    
if __name__ == '__main__':
    print(__doc__)
        
